package me.zixing.memorandum.domain;

import me.zixing.memorandum.utils.JdbcUtils;
import me.zixing.memorandum.utils.LunarUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

public class DayDO {

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    private Date date;
    private Integer month;
    private Integer dayOfMonth = 1;
    private Integer dayOfWeek = 0;
    private String dayForGregorian;
    private String dayForLunar;
    private List<DayTask> dayTasks;
    private List<DayTask> pastDayTasks;

    private DayDO() {
    }


    /**
     * 获取下一天
     * @return
     */
    public DayDO nextDay(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(this.date);
        calendar.add(Calendar.DAY_OF_MONTH,1);
        DayDO nextDay = buildDayDO(calendar);
        return nextDay;
    }

    /**
     * 获取前一天
     * @return
     */
    public DayDO previousDay(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(this.date);
        calendar.add(Calendar.DAY_OF_MONTH,-1);
        DayDO previousDay = buildDayDO(calendar);
        return previousDay;
    }

    /**
     * 去到指定间隔的一天
     * @param space 间隔，正数表示未来，负数表示以前
     * @return
     */
    public DayDO toADay(int space){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(this.date);
        calendar.add(Calendar.DAY_OF_MONTH,space);
        DayDO aDay = buildDayDO(calendar);
        return aDay;
    }


    public static DayDO buildDayDO(Calendar calendar){
        DayDO dayDO = new DayDO();
        dayDO.setDate(calendar.getTime());
        dayDO.setMonth(calendar.get(Calendar.MONTH) + 1);//月份从12月开始计
        dayDO.setDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
        dayDO.setDayOfWeek(calendar.get(Calendar.DAY_OF_WEEK));
        dayDO.setDayForGregorian(simpleDateFormat.format(dayDO.getDate()));

        dayDO.setDayTasks(JdbcUtils.getDayTasks(dayDO));
        dayDO.setPastDayTasks(JdbcUtils.getPostDayTasks(dayDO));
        dayDO.setDayForLunar(getLunarDay(dayDO.getDate()));
        return dayDO;
    }

    public static String getLunarDay(Date date){
        int year = Integer.valueOf(new SimpleDateFormat("yyyy").format(date));
        int month = Integer.valueOf(new SimpleDateFormat("MM").format(date));
        int day = Integer.valueOf(new SimpleDateFormat("dd").format(date));
        String lunarString = LunarUtils.oneDayOnlyDay(year,month,day);
        return lunarString;
    }
    /****Get and Set****/

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDayOfMonth() {
        return dayOfMonth;
    }

    public void setDayOfMonth(int dayOfMonth) {
        this.dayOfMonth = dayOfMonth;
    }

    public int getDayOfWeek() {
        return dayOfWeek;
    }

    public void setDayOfWeek(int dayOfWeek) {
        this.dayOfWeek = dayOfWeek;
    }

    public String getDayForGregorian() {
        return dayForGregorian;
    }

    public void setDayForGregorian(String dayForGregorian) {
        this.dayForGregorian = dayForGregorian;
    }

    public String getDayForLunar() {
        return dayForLunar;
    }

    public void setDayForLunar(String dayForLunar) {
        this.dayForLunar = dayForLunar;
    }

    public List<DayTask> getDayTasks() {
        return dayTasks;
    }

    public void setDayTasks(List<DayTask> dayTasks) {
        this.dayTasks = dayTasks;
    }

    public List<DayTask> getPastDayTasks() {
        return pastDayTasks;
    }

    public void setPastDayTasks(List<DayTask> pastDayTasks) {
        this.pastDayTasks = pastDayTasks;
    }

}
